// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//修改时间格式
import moment from 'moment'
Vue.prototype.$moment = moment;
//全局过滤器
Vue.filter('dateFmt', (input, formatString = "YYYY-MM-DD") => {
  //es5函数参数设置默认值
  //const lastFormatString = formatString || ''

  // moment(input) 把时间字符串转成时间对象
  // format(formatString) 把时间对象，按照指定格式，格式化成符合条件的字符串
  return moment(input).format(formatString)
});

//导入element
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueQriously from 'vue-qriously'

import VueDPlayer from "vue-dplayer";
import "vue-dplayer/dist/vue-dplayer.css";
Vue.use(VueDPlayer);



Vue.use(VueQriously);
Vue.config.productionTip = false;
Vue.use(ElementUI);
/* eslint-disable no-new */
import ax from 'axios'
Vue.prototype.$bus = new Vue();
//前台
const  axios1=ax.create({
  //基础的路径
  baseURL:"http://localhost:8899/",
  //headers:{"Authorization":sessionStorage.getItem("user")},
  // //携带到后台
  //withCredentials:true,

});
//后台
const  axios2=ax.create({
  //基础的路径
  baseURL:"http://localhost:8898/",
  //headers:{"Authorization":sessionStorage.getItem("user")},
  // //携带到后台
  //withCredentials:true,

});

//注册一个全局变量   是  $axios 前台
Vue.prototype.$axios=axios1;
//后台
Vue.prototype.$axios2=axios2;
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
});
//请求拦截器
/*axios2.interceptors.request.use(function (config) {
  let token =sessionStorage.getItem("user");
  //alert(JSON.stringify(token+"======"))
  if (token) {
    config.headers['Authorization'] = token;
  }
  return config;
})*/


//响应拦截器
/*axios2.interceptors.response.use(
  response => {
    let token =sessionStorage.getItem("user");
    //alert(JSON.stringify(token+"++++++"))
    if (response.data.code == 401) {
      //返回的code是1 的时候直接 跳转到login里面
      router.push({
        path:"/login2"
      })
    }else if (response.data.code == 402){
      alert(JSON.stringify(response.data.message));
      router.push({
        path:"/login2"
      })
    }
    return response;   // 返回请求回来的数据
  },
  err => {
    alert("错误的信息"+err);
    console.log(err)
  }
);*/




// // 跳转路由时回到页面顶部方法一（或者在index。js里配置）
router.afterEach((to,from,next) => {
  if(router.history.current.path==="/tttt"){
    console.log("不滚动到顶部");
  }else{
    window.scrollTo(0,0);
  }

});

//写一个前端拦截器
axios1.interceptors.response.use(res=>{

  if (res.data.code == 1){
    alert("登录失败");
    router.push({
      path:"/blogin"
    })
  }else if (res.data.code == 2){
    alert("登录失败");
    router.push({
      path:"/blogin"
    })
  } else {
    return res;
  }

},error => {
  // alert("开始")
  // router.push({
  //   path:"login"
  // })
}),

  //请求拦截器
  axios1.interceptors.request.use(function (config) {
    let token =sessionStorage.getItem("token");
    //如果路径请求不是登录,就 在请求头里添加token
    if (token && config.url.indexOf("login") < 0) {
      config.headers['Authorization'] = token;
    }
    return config;
  }),

//自定义指令
  Vue.directive('hasP',{
    inserted:function (el,binding){
      let z = sessionStorage.getItem("zy");
      if (!z.indexOf("user:add") > -1){
        el.setAttribute('style','display:none');
      }
    }
  })

Vue.directive('has', {
  inserted: function (el, binding) {
    if (!Vue.prototype.$_has(binding.value)) {
      el.parentNode.removeChild(el);
    }
  }
});
//权限检查方法
Vue.prototype.$_has = function (value) {
  let isExist = false;

  let premissionsStr = sessionStorage.getItem("zy");

  if (premissionsStr == undefined || premissionsStr == null) {
    return false;
  }

  if (premissionsStr.indexOf(value) > -1) {

    isExist = true;
  }
  return isExist;
};
